Creating a minimal Blockchain

This notebook is based on this blog post


In [4]:
import hashlib as hasher
import datetime as date

In [9]:
class Block:
    def __init__(self, index, timestamp, data, previous_hash):
        self.index = index
        self.timestamp = timestamp
        self.data = data
        self.previous_hash = previous_hash
        self.hash = self.hash_block()

    def hash_block(self):
        sha = hasher.sha256()
        sha.update(str(self.index).encode('utf8') +
                   str(self.timestamp).encode('utf8') +
                   str(self.data).encode('utf8') +
                   str(self.previous_hash).encode('utf8'))
        return sha.hexdigest()

In [10]:
def create_genesis_block():
    '''
    Manually construct a block with
    index zero and arbitrary previous hash
    '''
    return Block(0, date.datetime.now(), "Genesis Block", "0")

In [11]:
def next_block(last_block):
    this_index = last_block.index + 1
    this_timestamp = date.datetime.now()
    this_data = "Hey! I'm block " + str(this_index)
    this_hash = last_block.hash
    return Block(this_index, this_timestamp, this_data, this_hash)

In [12]:
# Create the blockchain and add the genesis block
blockchain = [create_genesis_block()]
previous_block = blockchain[0]

# How many blocks should we add to the chain
# after the genesis block
num_of_blocks_to_add = 20

# Add blocks to the chain
for i in range(0, num_of_blocks_to_add):
    block_to_add = next_block(previous_block)
    blockchain.append(block_to_add)
    previous_block = block_to_add
    # Tell everyone about it!
    print ("Block #{} has been added to the blockchain!".format(block_to_add.index))
    print ("Hash: {}\n".format(block_to_add.hash) )


Block #1 has been added to the blockchain!
Hash: be70bb6be9c8d790a7028606bb75df241c96db0adeb4696d3174e7817049c58e

Block #2 has been added to the blockchain!
Hash: c3f9a65c08622a3c1e57246d7774dafb3588812455ab75b6665c87a9f1a5b76a

Block #3 has been added to the blockchain!
Hash: 5a65b28847990b63738dfdc3d12db9a1671170c91666a8cfc9c2bd337f6d4ffa

Block #4 has been added to the blockchain!
Hash: daf02d6ec109fd1a1f931e0ed2f9cb3544bc70b508bfa3aba7b04177cf896c67

Block #5 has been added to the blockchain!
Hash: e75add99c98b3578223d68c2c57e955090efd9ef11d2cad61f70d3381d7dcb18

Block #6 has been added to the blockchain!
Hash: 11b160079071cbef3640c29be7b49359b3811a16fe2abb301f49116c8f9c5425

Block #7 has been added to the blockchain!
Hash: 40c9919edd970f2eb932369afa6bae4b7d862ec0df167710a107db9cec17fab2

Block #8 has been added to the blockchain!
Hash: a69ffeaee2141cc790f2d75f62224b22e3db8028eb60bb260df185ce5c49330a

Block #9 has been added to the blockchain!
Hash: ad5f4944e3d3f097b90dc427078a03e72b9d2338f80a840ee3d3bfd3fbe6006b

Block #10 has been added to the blockchain!
Hash: 3c7a0b0d7357c5c64604d9d144682c1a82d04125e1756009065b460152b0bdc8

Block #11 has been added to the blockchain!
Hash: 7d5e51bcd1ed42aaf77b2d5de4c92493af74ec783d7d8eabb97345ea3989fdfe

Block #12 has been added to the blockchain!
Hash: 191e783e5dde54f4d0e053551d1e9f255d81392709abe4fdd649974f64c935f2

Block #13 has been added to the blockchain!
Hash: 843532e2bcbae93f1bbdee47801099c4ad5edf716d7d01bb2eb33ec6c7856279

Block #14 has been added to the blockchain!
Hash: b53da2c0b83f50038135454682c157a0bb6f3a963901aeb560096902f1c13aa5

Block #15 has been added to the blockchain!
Hash: ca6ca5073a42dcb8c9f6fb0ad788ab06830dc860e23374514c3692561deea6a5

Block #16 has been added to the blockchain!
Hash: 9433fdebc2d8a2b6737eebd73ead64724b16993a3c060e2e6195b77e13f247d4

Block #17 has been added to the blockchain!
Hash: ac0e363cb3898dbd0c8da94ea4772fcf55527650dfa94c2b5ae4b0a722cb14fe

Block #18 has been added to the blockchain!
Hash: bf67b1791f58c7265165371fb439ff5b8ac12ede803005fe526f2cd73f460a60

Block #19 has been added to the blockchain!
Hash: 1069b68f491c80852e2d36413f25c8892df5157c98897c541ef7486d5ac2e4b4

Block #20 has been added to the blockchain!
Hash: 712423e9bbac77a5848d75c324c1cd752e7b658287b0079c1bb7b9a3af31a105

Homework:

Implement the rest of the system as on or more Python modules. The code is explained here.


In [ ]: